#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct EDGE {
    int u, v, w;
    EDGE() { u = 0, v = 0, w = 0; }
    EDGE(int u, int v, int w) { this->u = u, this->v = v, this->w = w; }
    bool operator<(const EDGE e) const { return w < e.w; }
    bool operator>(const EDGE e) const { return w > e.w; }
};

const int INF = 0x7fffffff;
const int MAXN = 5e3 + 10;
int N, M;
int ans = 0;
vector<bool> vis(MAXN, 0);
vector<vector<EDGE>> Adj(MAXN, vector<EDGE>(0));
typedef pair<int, int> PII;

int main() {
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        Adj[u].push_back(EDGE(u, v, w));
        Adj[v].push_back(EDGE(v, u, w));
    }

    priority_queue<EDGE, vector<EDGE>, greater<EDGE>> Q;
    Q.push(EDGE(0, 1, 0));
    while (!Q.empty()) {
        int u = Q.top().v;
        int w = Q.top().w;
        Q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        ans += w;
        for (auto it = Adj[u].begin(); it != Adj[u].end(); it++) {
            Q.push(*it);
        }
    }
    for (int i = 1; i <= N; i++)
        if (!vis[i]) {
            cout << "orz";
            return 0;
        }
    cout << ans;
}